package 单周赛.september;


import java.util.ArrayList;
import java.util.List;

/**
 * @
 * @date 2024/03/03
 */
public class 第387场单周赛 {

    public static void main(String[] args) {

        int[][] gird = {
                {1, 2, 2}, {1, 1, 0}, {0, 1, 0}
        };
        int[][] gird2 = {
                {0, 1, 0, 1, 0}, {2, 1, 0, 1, 2}, {2, 2, 2, 0, 1}, {2, 2, 2, 2, 2}, {2, 1, 2, 2, 2}
        };
        System.out.println(minimumOperationsToWriteY(gird2));

    }

    public int[] resultArray(int[] nums) {
        List<Integer> arr1 = new ArrayList<>();
        List<Integer> arr2 = new ArrayList<>();

        arr1.add(nums[0]);
        arr2.add(nums[1]);

        for (int i = 2; i < nums.length; i++) {
            if (arr1.get(arr1.size() - 1) > arr2.get(arr2.size() - 1)) {
                arr1.add(nums[i]);
            } else {
                arr2.add(nums[i]);
            }
        }

        int[] result = new int[nums.length];
        for (int i = 0; i < arr1.size(); i++) {
            result[i] = arr1.get(i);
        }
        for (int i = arr1.size(); i < arr1.size() + arr2.size(); i++) {
            result[i] = arr2.get(i - arr1.size());
        }

        return result;
    }

    public int countSubmatrices(int[][] grid, int k) {
        int cnt = 0;
        int[] preSum = new int[grid[0].length];

        for (int i = 0; i < grid.length; i++) {

            long pre = 0;
            for (int j = 0; j < grid[0].length; j++) {

                if (pre + preSum[j] + grid[i][j] <= k) {
                    cnt++;
                }

                pre += preSum[j] + grid[i][j];
                preSum[j] += grid[i][j];
            }
        }


        return cnt;
    }


    public static int minimumOperationsToWriteY(int[][] grid) {
        int[] yArr = new int[3], otherArr = new int[3];

        int rows = grid.length, columns = grid[0].length;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {

                if (i < rows / 2 && (i == j || i + j == columns - 1) ||
                        i >= rows / 2 && j == columns / 2) {

                    yArr[grid[i][j]]++;
                    grid[i][j] = -1;
                } else {
                    otherArr[grid[i][j]]++;
                }
            }
        }
        int ans = Integer.MAX_VALUE;

        for (int i = 0; i <= 2; i++) {
            for (int j = 0; j <= 2; j++) {
                if (i == j) {
                    continue;
                }

                int sum = 0;
                for (int r = 0; r < yArr.length; r++) {
                    if (r != i) {
                        sum += yArr[r];
                    }
                }
                for (int r = 0; r < otherArr.length; r++) {
                    if (r != j) {
                        sum += otherArr[r];
                    }
                }

                ans = Math.min(ans, sum);
            }
        }

        return ans;
    }

}

